package com.p2l.letterblocks;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.util.Xml;

public class XmlParser {
	public static final int TYPE_BEAR = 0;
	public static final int TYPE_COIN = 1;
	public static final int TYPE_BLOCK = 2;
	public static final int TYPE_OBSTACLE = 3;
	
	public static XmlPullParser getXmlParser(GamePersistor persistor, InputStream in) throws XmlPullParserException {
		XmlPullParser parser = Xml.newPullParser();
    	parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
    	parser.setInput(in, "UTF-8");
    	return parser;
	}
    
	public static Bitmap getBackground(XmlPullParser parser, GamePersistor persistor) throws XmlPullParserException, IOException {
		parser.nextTag();
    	parser.require(XmlPullParser.START_TAG, null, "Level");
    	
    	String background = "";
    	// go through parser until we get to a end tag
    	while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG) {
    			continue;
    		}
    		
    		// if tag name is equal to Background
    		String name = parser.getName();
    		if (name.equals("Background")) {
    			background = getString(parser, "Background");
    			break;
    		} else {
    			getString(parser, name);
    		}
    		name = null;
    	}
    	
    	// find unique id of the background image
    	int id = persistor.getResources().getIdentifier(background , "drawable", persistor.getPackageName());
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inScaled = false;
		
		// get the image from resources and return it
		Bitmap bitmap = BitmapFactory.decodeResource(persistor.getResources(), id, options);
		background = null;
		options = null;
		return bitmap;
	}
	
	public static Bitmap getBackgroundLayer(XmlPullParser parser, GamePersistor persistor) throws XmlPullParserException, IOException {
    	String layer = "";
    	// go through parser until we get to a end tag
    	while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG) {
    			continue;
    		}
    		
    		// if tag name is equal to Layer
    		String name = parser.getName();
    		if (name.equals("Layer")) {
    			layer = getString(parser, "Layer");
    			break;
    		} else {
    			getString(parser, name);
    		}
    		name = null;
    	}
    	
    	// find unique id of the background layer image
    	int id = persistor.getResources().getIdentifier(layer , "drawable", persistor.getPackageName());
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inScaled = false;
		
		// get the image from resources and return it
		Bitmap bitmap = BitmapFactory.decodeResource(persistor.getResources(), id, options);
		options = null;
		layer = null;
		return bitmap;
	}
	
	public static Bitmap getFloor(XmlPullParser parser, GamePersistor persistor) throws XmlPullParserException, IOException {
    	String floor = "";
    	// go through parser until we get to a end tag
    	while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG) {
    			continue;
    		}
    		
    		// if tag name is equal to Floor
    		String name = parser.getName();
    		if (name.equals("Floor")) {
    			floor = getString(parser, "Floor");
    			break;
    		} else {
    			getString(parser, name);
    		}
    		name = null;
    	}
    	
    	// find unique id of the floor image
    	int id = persistor.getResources().getIdentifier(floor , "drawable", persistor.getPackageName());
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inScaled = false;
		
		// get the image from resources and return it
		Bitmap bitmap = BitmapFactory.decodeResource(persistor.getResources(), id, options);
		options = null;
		return bitmap;
	}
	
	public static String getWord(XmlPullParser parser, GamePersistor persistor) throws XmlPullParserException, IOException {
    	String word = "";
    	// go through parser until we get to a end tag
    	while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG) {
    			continue;
    		}
    		
    		// if tag name is equal to Word
    		String name = parser.getName();
    		if (name.equals("Word")) {
    			word = getString(parser, "Word");
    			break;
    		} else {
    			getString(parser, name);
    		}
    		name = null;
    	}
    	
		return word;
	}
	
	public static ArrayList<Sprite> getSprites(XmlPullParser parser, GamePersistor persistor) throws XmlPullParserException, IOException {
		ArrayList<Sprite> sprites = new ArrayList<Sprite>();
		
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG) {
    			continue;
    		}
    		
    		// if tag name is equal to Object
    		String name = parser.getName();
    		if (name.equals("Object")) {
    			// call method-parseObject to parse the content to an object
    			Sprite s = parseObject(parser, persistor);
    			// add object to ArrayList of Sprite's
    			sprites.add(s);
    		} else {
    			getString(parser, name);
    		}
    		name = null;
    	}
		
		return sprites;
	}
	
	public static Sprite parseObject(XmlPullParser parser, GamePersistor persistor) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "Object");
		
		SharedPreferences preferences = persistor.getSharedPreferences("LetterBlocks", Activity.MODE_PRIVATE);
		int language = preferences.getInt("language", 0);
		
		String [] VOWELS = persistor.getResources().getStringArray(R.array.vowels_en);
		switch (language) {
		case LevelLoader.ENGLISH:
			VOWELS = persistor.getResources().getStringArray(R.array.vowels_en);
			break;
		case LevelLoader.SPANISH:
			VOWELS = persistor.getResources().getStringArray(R.array.vowels_es);
			break;
		case LevelLoader.GERMAN:
			VOWELS = persistor.getResources().getStringArray(R.array.vowels_de);
			break;
		case LevelLoader.DANISH:
			VOWELS = persistor.getResources().getStringArray(R.array.vowels_da);
			break;
		default:
			VOWELS = persistor.getResources().getStringArray(R.array.vowels_en);
			break;
		}
		
		String image = "";
		float x = -1f;
		float y = -1f;
		float height = 0f;
		float width = 0f;
		char letter = ' ';
		int type = -1;
		
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			
			// save the data in local variables
			String name = parser.getName();
			if (name.equals("X")) {
				x = getRight(getFloat(parser, "X"));
			} else if (name.equals("Y")) {
				y = getBottom(getFloat(parser, "Y"));
			} else if (name.equals("Height")) {
				height = getFloat(parser, "Height");
			} else if (name.equals("Width")) {
				width = getFloat(parser, "Width");
			} else if (name.equals("Image")) {
				image = getString(parser, "Image");
			} else if (name.equals("Letter")) {
				letter = getChar(parser, "Letter");
			} else if (name.equals("Type")) {
				type = getInt(parser, "Type");
			} else {
    			getString(parser, name);
    		}
			name = null;
		}
		
		// find unique id of the objects image
		int id = persistor.getResources().getIdentifier(image , "drawable", persistor.getPackageName());
		// get the image from resources
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inScaled = false;
		Bitmap bitmap = BitmapFactory.decodeResource(persistor.getResources(), id, options);
		options = null;
		
		switch (type) {
		// if object is a type of Bear
		case TYPE_BEAR:
			Bear bear = new Bear(x, y, height, width, bitmap);
			bear.TYPE = TYPE_BEAR;
			return bear;
		// if object is a type of Coin 		
		case TYPE_COIN:
			Coin coin = new Coin(x, y, height, width, bitmap);
			coin.TYPE = TYPE_COIN;
			return coin;
		// if object is a type of Block
		case TYPE_BLOCK:
			// linear search to check if letter is a vowel or a consonant
			boolean found = false;
			int i = 0;
			while (!found && i < VOWELS.length) {
				if (letter == VOWELS[i].charAt(0))
					found = true;
				else
					i++;
			}
			
			Block block = new Block(persistor, x, y, height, width, bitmap, letter, GamePersistor.SCALE, (found ? Color.RED : Color.BLUE));
			block.TYPE = TYPE_BLOCK;
			return block;
		// if object is a type of Obstacle
		case TYPE_OBSTACLE:
			Obstacle obstacle = new Obstacle(x, y, height, width, bitmap);
			obstacle.TYPE = TYPE_OBSTACLE;
			return obstacle;
		}
		image = null;
		
		return null;
	}
	
	public static ArrayList<Item> getItems(XmlPullParser parser) throws XmlPullParserException, IOException {
		ArrayList<Item> items = new ArrayList<Item>();
		
		parser.nextTag();
    	parser.require(XmlPullParser.START_TAG, null, "Items");
    	
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG) {
    			continue;
    		}
    		
    		// if tag name is equal to Item
    		String name = parser.getName();
    		if (name.equals("Item")) {
    			// call method-parseObject to parse the content to an item
    			Item item = parseItem(parser);
    			// add item to ArrayList of items
    			items.add(item);
    		} else {
    			getString(parser, name);
    		}
    		name = null;
		}
		return items;
	}
	
	public static Item parseItem(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "Item");
		
		String[] description = new String[4];
		String itemName = "";
		int price = -1;
		int type = -1;
		int id = -1;
		
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			
			// save the data in local variables
			String name = parser.getName();
			if (name.equals("Name"))
				itemName = getString(parser, "Name");
			else if (name.equals("Price"))
				price = getInt(parser, "Price");
			else if (name.equals("Type"))
				type = getInt(parser, "Type");
			else if (name.equals("Id"))
				id = getInt(parser, "Id");
			else if (name.equals("En"))
				description[0] = getString(parser, "En");
			else if (name.equals("Es"))
				description[1] = getString(parser, "Es");
			else if (name.equals("De"))
				description[2] = getString(parser, "De");
			else if (name.equals("Dk"))
				description[3] = getString(parser, "Dk");
			else
				getString(parser, name);
			name = null;
		}
		return new Item(description, itemName, price, id, type);
	}
	
	public static int login(XmlPullParser parser) throws XmlPullParserException, IOException {
		int id = -1;
		
		parser.nextTag();
    	parser.require(XmlPullParser.START_TAG, null, "User");
    	
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG) {
    			continue;
    		}
    		
    		// if tag name is equal to Id
    		String name = parser.getName();
    		if (name.equals("Id"))
    			id = getInt(parser, "Id");
    		else
    			getString(parser, name);
    		name = null;
		}
		return id;
	}
	
	public static ArrayList<User> getUsers(XmlPullParser parser) throws XmlPullParserException, IOException {
		ArrayList<User> users = new ArrayList<User>();
		
		parser.nextTag();
    	parser.require(XmlPullParser.START_TAG, null, "Users");
    	
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG) {
    			continue;
    		}
    		
    		// if tag name is equal to User
    		String name = parser.getName();
    		if (name.equals("User"))
    			users.add(parseUser(parser));
    		else
    			getString(parser, name);
    		name = null;
		}
		return users;
	}
	
	public static User parseUser(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "User");
		
		String username = "";
		String password = "";
		String email = "";
		int id = -1;
		
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			
			// save the data in local variables
			String name = parser.getName();
			if (name.equals("Username"))
				username = getString(parser, "Username");
			else if (name.equals("Password"))
				password = getString(parser, "Password");
			else if (name.equals("Email"))
				email = getString(parser, "Email");
			else if (name.equals("Id"))
				id = getInt(parser, "Id");
			else
    			getString(parser, name);
			name = null;
		}
		return new User(username, password, email, id);
	}
	
	public static ArrayList<Chapter> getChapters(XmlPullParser parser) throws XmlPullParserException, IOException {
		ArrayList<Chapter> chapters = new ArrayList<Chapter>();
		
		parser.nextTag();
    	parser.require(XmlPullParser.START_TAG, null, "Chapters");
    	
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG) {
    			continue;
    		}
    		
    		// if tag name is equal to Chapter
    		String name = parser.getName();
    		if (name.equals("Chapter"))
    			chapters.add(parseChapter(parser));
    		else
    			getString(parser, name);
    		name = null;
		}
		return chapters;
	}
	
	public static Chapter parseChapter(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "Chapter");
		
		String[] description = new String[4];
		String cName = "";
		int price = -1;
		int id = -1;
		
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			
			// save the data in local variables
			String name = parser.getName();
			if (name.equals("Name"))
				cName = getString(parser, "Name");
			else if (name.equals("Price"))
				price = getInt(parser, "Price");
			else if (name.equals("Id"))
				id = getInt(parser, "Id");
			else if (name.equals("En"))
				description[0] = getString(parser, "En");
			else if (name.equals("Es"))
				description[1] = getString(parser, "Es");
			else if (name.equals("De"))
				description[2] = getString(parser, "De");
			else if (name.equals("Dk"))
				description[3] = getString(parser, "Dk");
			else
    			getString(parser, name);
			name = null;
		}
		return new Chapter(id, cName, price, description);
	}
	
	public static ArrayList<Level> getLevels(XmlPullParser parser) throws XmlPullParserException, IOException {
		ArrayList<Level> levels = new ArrayList<Level>();
		
		parser.nextTag();
    	parser.require(XmlPullParser.START_TAG, null, "Levels");
    	
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG) {
    			continue;
    		}
    		
    		// if tag name is equal to Level
    		String name = parser.getName();
    		if (name.equals("Level"))
    			levels.add(parseLevel(parser));
    		else
    			getString(parser, name);
    		name = null;
		}
		return levels;
	}
	
	public static Level parseLevel(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "Level");
		
		String background = "";
		String layer = "";
		String floor = "";
		String dk = "";
		String en = "";
		String de = "";
		String es = "";
		int chapterId = -1;
		int id = -1;
		
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			
			// save the data in local variables
			String name = parser.getName();
			if (name.equals("Background"))
				background = getString(parser, "Background");
			else if (name.equals("Layer"))
				layer = getString(parser, "Layer");
			else if (name.equals("Floor"))
				floor = getString(parser, "Floor");
			else if (name.equals("DK"))
				dk = getString(parser, "DK");
			else if (name.equals("EN"))
				en = getString(parser, "EN");
			else if (name.equals("DE"))
				de = getString(parser, "DE");
			else if (name.equals("ES"))
				es = getString(parser, "ES");
			else if (name.equals("ChapterId"))
				chapterId = getInt(parser, "ChapterId");
			else if (name.equals("Id"))
				id = getInt(parser, "Id");
			else
    			getString(parser, name);
			name = null;
		}
		return new Level(id, chapterId, background, layer, floor, dk, en, de, es);
	}
	
	public static ArrayList<P2LObject> getObjects(XmlPullParser parser) throws XmlPullParserException, IOException {
		ArrayList<P2LObject> objects = new ArrayList<P2LObject>();
		
		parser.nextTag();
    	parser.require(XmlPullParser.START_TAG, null, "Objects");
    	
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG) {
    			continue;
    		}
    		
    		// if tag name is equal to Level
    		String name = parser.getName();
    		if (name.equals("Object"))
    			objects.add(parseObject(parser));
    		else
    			getString(parser, name);
    		name = null;
		}
		return objects;
	}
	
	public static P2LObject parseObject(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "Object");
		
		String image = "";
		float x = -1f;
		float y = -1f;
		float height = -1f;
		float width = -1f;
		int levelId = -1;
		int type = -1;
		int id = -1;
		
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			
			// save the data in local variables
			String name = parser.getName();
			if (name.equals("Image"))
				image = getString(parser, "Image");
			else if (name.equals("X"))
				x = getFloat(parser, "X");
			else if (name.equals("Y"))
				y = getFloat(parser, "Y");
			else if (name.equals("Height"))
				height = getFloat(parser, "Height");
			else if (name.equals("Width"))
				width = getFloat(parser, "Width");
			else if (name.equals("LevelId"))
				levelId = getInt(parser, "LevelId");
			else if (name.equals("Type"))
				type = getInt(parser, "Type");
			else if (name.equals("Id"))
				id = getInt(parser, "Id");
			else
    			getString(parser, name);
			name = null;
		}
		return new P2LObject(id, levelId, x, y, height, width, type, image);
	}
	
	public static ArrayList<LevelCompleted> getLevelsCompleted(XmlPullParser parser) throws XmlPullParserException, IOException {
		ArrayList<LevelCompleted> levels_completed = new ArrayList<LevelCompleted>();
		
		parser.nextTag();
    	parser.require(XmlPullParser.START_TAG, null, "LevelsCompleted");
    	
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG) {
    			continue;
    		}
    		
    		// if tag name is equal to Level
    		String name = parser.getName();
    		if (name.equals("Level"))
    			levels_completed.add(parseLevelCompleted(parser));
    		else
    			getString(parser, name);
    		name = null;
		}
		return levels_completed;
	}
	
	public static LevelCompleted parseLevelCompleted(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "Level");
		
		boolean coin = false;
		int levelId = -1;
		int userId = -1;
		int id = -1;
		int language = -1;
		
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			
			// save the data in local variables
			String name = parser.getName();
			if (name.equals("Coin"))
				coin = getBoolean(parser, "Coin");
			else if (name.equals("LevelId"))
				levelId = getInt(parser, "LevelId");
			else if (name.equals("UserId"))
				userId = getInt(parser, "UserId");
			else if (name.equals("Language"))
				language = getInt(parser, "Language");
			else if (name.equals("Id"))
				id = getInt(parser, "Id");
			else 
    			getString(parser, name);
			name = null;
		}
		return new LevelCompleted(id, levelId, userId, language, coin);
	}
	
	public static ArrayList<ChapterTransaction> getChapterTransactions(XmlPullParser parser) throws XmlPullParserException, IOException {
		ArrayList<ChapterTransaction> chapter_transactions = new ArrayList<ChapterTransaction>();
		
		parser.nextTag();
    	parser.require(XmlPullParser.START_TAG, null, "ChapterTransactions");
    	
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG) {
    			continue;
    		}
    		
    		// if tag name is equal to Level
    		String name = parser.getName();
    		if (name.equals("ChapterTransaction"))
    			chapter_transactions.add(parseChapterTransaction(parser));
    		else
    			getString(parser, name);
    		name = null;
		}
		return chapter_transactions;
	}
	
	public static ChapterTransaction parseChapterTransaction(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "ChapterTransaction");
		
		int chapterId = -1;
		int userId = -1;
		int price = -1;
		int id = -1;
		
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			
			// save the data in local variables
			String name = parser.getName();
			if (name.equals("Price"))
				price = getInt(parser, "Price");
			else if (name.equals("ChapterId"))
				chapterId = getInt(parser, "ChapterId");
			else if (name.equals("UserId"))
				userId = getInt(parser, "UserId");
			else if (name.equals("Id"))
				id = getInt(parser, "Id");
			else
    			getString(parser, name);
			name = null;
		}
		return new ChapterTransaction(id, chapterId, userId, price);
	}
	
	public static ArrayList<Login> getLogins(XmlPullParser parser) throws XmlPullParserException, IOException {
		ArrayList<Login> logins = new ArrayList<Login>();
		
		parser.nextTag();
    	parser.require(XmlPullParser.START_TAG, null, "Logins");
    	
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
    		if (parser.getEventType() != XmlPullParser.START_TAG) {
    			continue;
    		}
    		
    		// if tag name is equal to Level
    		String name = parser.getName();
    		if (name.equals("Login"))
    			logins.add(parseLogin(parser));
    		else
    			getString(parser, name);
    		name = null;
		}
		return logins;
	}
	
	public static Login parseLogin(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "Login");
		
		String date = "";
		String ipaddress = "";
		int userId = -1;
		
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			
			// save the data in local variables
			String name = parser.getName();
			if (name.equals("IPAddress"))
				ipaddress = getString(parser, "IPAddress");
			else if (name.equals("Date"))
				date = getString(parser, "Date");
			else if (name.equals("UserId"))
				userId = getInt(parser, "UserId");
			else
    			getString(parser, name);
			name = null;
		}
		
		Login login = new Login(date, userId);
		login.setIpaddress(ipaddress);
		return login;
	}
	
	public static int getCoinScore(XmlPullParser parser) throws XmlPullParserException, IOException {
		int result = 0;
		
		parser.nextTag();
    	parser.require(XmlPullParser.START_TAG, null, "Coins");

		String name = parser.getName();
		if (name.equals("Coins"))
			result = getInt(parser, "Coins");
		else
			getString(parser, name);
		name = null;
		
		return result;
	}
	
	// get the string from the tag
	public static String getString(XmlPullParser parser, String tag) throws XmlPullParserException, IOException {
		String res = "";
		parser.require(XmlPullParser.START_TAG, null, tag);
		if (parser.next() == XmlPullParser.TEXT) {
			res = parser.getText();
			parser.nextTag();
		}
		parser.require(XmlPullParser.END_TAG, null, tag);
		return res;
	}
	
	// parse the string from the tag to char
	public static char getChar(XmlPullParser parser, String tag) throws XmlPullParserException, IOException {
		String s = getString(parser, tag);
		if (s.length() >= 1)
			return s.charAt(0);
		else
			return ' ';
	}
	
	// parse the string from the tag to float
	public static float getFloat(XmlPullParser parser, String tag) throws XmlPullParserException, IOException {
		try {
			float f = Float.parseFloat(getString(parser, tag));
			return f;
		} catch (NumberFormatException e) {
			return -1f;
		}
	}
	
	// parse the string from the tag to float
	public static double getDouble(XmlPullParser parser, String tag) throws XmlPullParserException, IOException {
		try {
			double d = Double.parseDouble(getString(parser, tag));
			return d;
		} catch (NumberFormatException e) {
			return -1;
		}
	}
	
	// parse the string from the tag to int
	public static int getInt(XmlPullParser parser, String tag) throws XmlPullParserException, IOException {
		try {
			int n = Integer.parseInt(getString(parser, tag));
			return n;
		} catch (NumberFormatException e) {
			return -1;
		}
	}
	
	// parse the string from the tag to boolean
	public static boolean getBoolean(XmlPullParser parser, String tag) throws XmlPullParserException, IOException {
		try {
			String bool = getString(parser, tag);
			if (bool.equals("True")) {
				return true;
			} else {
				int n = Integer.parseInt(bool);
				return n == 1;
			}
		} catch (NumberFormatException e) {
			return false;
		}
	}
	
	public static float getBottom(float height) {
		return GamePersistor.SCREEN_HEIGHT - (GamePersistor.SCREEN_HEIGHT / 100f * height) - (GamePersistor.SCREEN_HEIGHT / 100f * GameView.BOTTOM);
	}
	
	public static float getRight(float right) {
		return GameView.GAME_WIDTH / 100f * right;
	}
}
